/*
 * UploadTestPanel.cpp
 *
 *  Created on: 2012-11-3
 *      Author: tutu
 */

#include "UploadTestPanel.h"
#include <QFileDialog>
#include <QMessageBox>
#include "../TContext.h"

UploadTestPanel::UploadTestPanel(QWidget *parent) : QWidget(parent)
{
	_titleInput = new QLineEdit(this);
	_titleInput->setText("Input Title");
	_titleInput->setGeometry(10, 10, 200, 30);

	_sharedCheckBox = new QCheckBox(this);
	_sharedCheckBox->setText("Shared");
	_sharedCheckBox->setGeometry(10, 50, 100, 30);

	_pickButton = new QPushButton(this);
	_pickButton->setText("Pick File");
	_pickButton->setGeometry(10, 90, 100, 30);
	connect(_pickButton, SIGNAL(clicked()), this, SLOT(pickButtonClicked()));

	_uploadButton = new QPushButton(this);
	_uploadButton->setText("Upload");
	_uploadButton->setGeometry(10, 130, 100, 30);
//	connect(_uploadButton, SIGNAL(clicked()), this, SLOT(uploadButtonClicked()));

	_advUploadButton = new QPushButton(this);
	_advUploadButton->setText("Adv Upload");
	_advUploadButton->setGeometry(10, 170, 100, 30);
	connect(_advUploadButton, SIGNAL(clicked()), this, SLOT(advUploadButtonClicked()));

//	_requester = TContext::instance()->requesterFactory()->createUploadRequester();
//	connect(_requester.data(), SIGNAL(uploadFinished(bool, TDocumentPointer)), this, SLOT(uploadFinished(bool, TDocumentPointer)));

    _advRequester = TContext::instance()->requesterFactory()->createAdvUploadRequester();
    connect(_advRequester.data(), SIGNAL(uploadFinished(bool, TDocumentPointer)), this, SLOT(uploadFinished(bool, TDocumentPointer)));
}

UploadTestPanel::~UploadTestPanel()
{
	// TODO Auto-generated destructor stub
}

void UploadTestPanel::pickButtonClicked()
{
	_filename = QFileDialog::getOpenFileName(this, tr("Open File"),
	                                                 "C:/",
	                                                 tr("Files (*.*)"));
}

void UploadTestPanel::uploadButtonClicked()
{
	if(_filename.isEmpty())
	{
		QMessageBox::warning(NULL, "Error", "No file");
		return;
	}
	if(_titleInput->text().isEmpty())
	{
		QMessageBox::warning(NULL, "Error", "No title");
		return;
	}
	_requester->upload(_filename, _titleInput->text(), _sharedCheckBox->isChecked());
}

void UploadTestPanel::advUploadButtonClicked()
{
	if(_filename.isEmpty())
	{
		QMessageBox::warning(NULL, "Error", "No file");
		return;
	}
	if(_titleInput->text().isEmpty())
	{
		QMessageBox::warning(NULL, "Error", "No title");
		return;
	}
    _advRequester->upload(_filename, _titleInput->text(), _sharedCheckBox->isChecked());
}

void UploadTestPanel::uploadFinished(bool error, TDocumentPointer document)
{
	if(!error)
	{
		QMessageBox::warning(NULL, "Result", "Upload Success. Document Id is "+QString::number(document->getId()));
	}
	else
	{
		QMessageBox::warning(NULL, "Result", "Upload Failure.");
	}
}
